<?php

/**
 * This is the model class for table "{{account_token_item}}".
 *
 * The followings are the available columns in table '{{account_token_item}}':
 * @property integer $id
 * @property integer $tokenId
 * @property integer $categoryId
 * @property string $summary
 * @property string $debit
 * @property string $credit
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property AccountToken $token
 * @property AccountTokenCategory $category
 * @property User $createUser
 * @property User $updateUser
 */
class AccountTokenItem extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return AccountTokenItem the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{account_token_item}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('summary, categoryId', 'required'),
			array('categoryId', 'numerical', 'integerOnly'=>true),
			array('summary', 'length', 'max'=>255),
			array('debit, credit', 'length', 'max'=>16),
			array('debit, credit', 'default', 'value'=>null),
			array('debit, credit', 'numerical'),
			//array('createTime, updateTime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, tokenId, categoryId, summary, debit, credit, createTime, createUserId, updateTime, updateUserId', 'safe', 'on'=>'search'),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'tokenId, categoryId, summary, debit, credit'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'token' => array(self::BELONGS_TO, 'AccountToken', 'tokenId'),
			'category' => array(self::BELONGS_TO, 'AccountTokenCategory', 'categoryId'),
			'createUser' => array(self::BELONGS_TO, 'User', 'createUserId'),
			'updateUser' => array(self::BELONGS_TO, 'User', 'updateUserId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tokenId' => 'Token',
			'categoryId' => 'Category',
			'summary' => 'Summary',
			'debit' => 'Debit',
			'credit' => 'Credit',
			'createTime' => 'Create Time',
			'createUserId' => 'Create User',
			'updateTime' => 'Update Time',
			'updateUserId' => 'Update User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('tokenId',$this->tokenId);
		$criteria->compare('categoryId',$this->categoryId);
		$criteria->compare('summary',$this->summary,true);
		$criteria->compare('debit',$this->debit,true);
		$criteria->compare('credit',$this->credit,true);
		$criteria->compare('createTime',$this->createTime,true);
		$criteria->compare('createUserId',$this->createUserId);
		$criteria->compare('updateTime',$this->updateTime,true);
		$criteria->compare('updateUserId',$this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}